package com.zy.pro.scroll.web;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.city.model.City;
import com.zy.pro.city.service.CityService;
import com.zy.pro.city.web.form.CitySearchForm;
import com.zy.pro.scroll.model.ScrollImage;
import com.zy.pro.scroll.service.ScrollImageService;
import com.zy.pro.scroll.web.form.ScrollImageSearchForm;
import com.zy.pro.shop.model.Cases;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.service.CasesService;
import com.zy.pro.shop.service.ShopService;
import com.zy.pro.shop.web.form.CasesSearchForm;
import com.zy.pro.shop.web.form.ShopSearchForm;
import com.zy.utils.UploadHelper;
/**
 * 首页滚动图管理控制器
 *
 */
@Controller
@RequestMapping("/control/scroll/scrollImageController")
public class ScrollImageController extends BaseController {

	private static final Logger logger = Logger.getLogger(ScrollImageController.class);
	
	/**
	 * 首页滚动图管理服务类
	 */
	@Resource(name = "scrollImageServiceImpl")
	private ScrollImageService scrollImageService;
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopServiceImpl")
	private ShopService shopService;
	
	/**
	 * 案例管理服务类
	 */
	@Resource(name = "casesServiceImpl")
	private CasesService casesService;
	
	/**
	 * 城市管理服务类
	 */
	@Resource(name = "cityServiceImpl")
	private CityService cityService;
	
	/**
	 * 系统-列出首页滚动图管理
	 */
	@RequestMapping("/sysScrollList")
	public String sysScrollList(ScrollImageSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			if(form.getScrollName() != null && form.getScrollName() != ""){
				form.setScrollName(form.getScrollName().trim());
			}
			
			form.getOrderby().put("scrollCreateTime", "desc");
			
			//创建页面对象
			PageView<ScrollImage> pageView = new PageView<ScrollImage>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(scrollImageService.getData(form));
			
			CitySearchForm cForm = new CitySearchForm();
			cForm.getOrderby().put("cityCreateTime", "desc");
			List<City> cityList = cityService.findAll(cForm);
			
			map.addAttribute("cityList", cityList);
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			map.addAttribute("reqUrl", "/control/scroll/scrollImageController/sysScrollList.do");
			//TODO 转发页面
			return "control/system/scroll/sysScrollList";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-编辑首页滚动图状态
	 */
	@RequestMapping("/editStatus")
	public void editStatus(ScrollImageSearchForm form, ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			ScrollImage s = scrollImageService.find(form.getScrollId());
			if(null!=s){
				ScrollImage scroll = new ScrollImage();
				scroll.setScrollId(form.getScrollId());
				scroll.setScrollStatus(form.getScrollStatus());//1:正常,2:冻结,3:删除
				scrollImageService.updateBySelective(scroll);
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	/**
	 * 系统-显示添加首页滚动图管理
	 */
	@RequestMapping("/dispalySysAdd")
	public String dispalySysAdd(ScrollImage scroll, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			map.addAttribute("operateStatus", "sysAdd");
			map.addAttribute("scroll", scroll);
			//TODO 转发页面
			return "/control/system/scroll/sysEditScroll";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-显示编辑首页滚动图管理
	 */
	@RequestMapping("/dispalySysEdit")
	public String dispalySysEdit(ScrollImage scroll, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			scroll = scrollImageService.find(scroll.getScrollId());
			map.addAttribute("operateStatus", "sysEdit");
			map.addAttribute("scroll", scroll);
			//TODO 转发页面
			return "/control/system/scroll/sysEditScroll";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	

	/**
	 * 系统-添加首页滚动图管理
	 */
	@RequestMapping("/sysAdd")
	public String sysAdd(ScrollImage scroll, ModelMap map,HttpServletRequest request) {
		try {
			//TODO 设置shop  eg:主键信息
			
			if(scroll.getScrollPath() != null && scroll.getScrollPath().indexOf("yibu") != -1){
				//将异步上传的Logo拷贝到正式路径下面
				String newPath = scroll.getScrollPath().replaceAll("yibu", "scrollPath");
				UploadHelper.copyFile(scroll.getScrollPath(), newPath, "yibu", request, logger);
				scroll.setScrollPath(newPath);
				
			}
			
			scroll.setScrollStatus(1);
			scroll.setScrollCreateTime(new Date());
			//判断对象的类型(1:店铺,2:案例)
			if(scroll.getScrollType() != null && scroll.getScrollType() == 1){
				scroll.setScrollObjPath("/control/shop/shopController/shopDetail.do?shopId="+scroll.getScrollObjId().toString());
				/*//设置城市主键
				if(scroll.getScrollObjId() != null){
					Shop shop = shopService.find(scroll.getScrollObjId());
					scroll.setCityId(shop.getCityId());
				}*/
			}else if(scroll.getScrollType() != null && scroll.getScrollType() == 2){
				scroll.setScrollObjPath("/control/cases/casesController/casesDetail.do?casesId="+scroll.getScrollObjId().toString());
				/*//设置城市主键
				if(scroll.getScrollObjId() != null){
					Cases cases = casesService.find(scroll.getScrollObjId());
					Shop shop = shopService.find(cases.getCaseShopId());
					scroll.setCityId(shop.getCityId());
				}*/
			}
			scrollImageService.save(scroll);
			return sysScrollList(new ScrollImageSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-编辑首页滚动图管理
	 */
	@RequestMapping("/sysEdit")
	public String sysEdit(ScrollImage scroll,String oldScrollPath,ModelMap map, HttpServletRequest request) {
		try {
			//TODO:处理店铺管理
			if(scroll.getScrollPath() != null && scroll.getScrollPath().indexOf("yibu") != -1){
				//将异步上传的Logo拷贝到正式路径下面
				String newPath = scroll.getScrollPath().replaceAll("yibu", "scrollPath");
				UploadHelper.copyFile(scroll.getScrollPath(), newPath, "yibu", request, logger);
				scroll.setScrollPath(newPath);
				
				//删除旧Logo
				//UploadHelper.deletePic(oldShopLogo, "shopLogo", request, logger);
			}
			
			//判断对象的类型(1:店铺,2:案例)
			if(scroll.getScrollType() != null && scroll.getScrollType() == 1){
				scroll.setScrollObjPath("/control/shop/shopController/shopDetail.do?shopId="+scroll.getScrollObjId().toString());
			}else if(scroll.getScrollType() != null && scroll.getScrollType() == 2){
				scroll.setScrollObjPath("/control/cases/casesController/casesDetail.do?casesId="+scroll.getScrollObjId().toString());
			}
			
			scrollImageService.updateBySelective(scroll);
			return sysScrollList(new ScrollImageSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	/**
	 * 系统-根据选择的图片类型加载 店铺/案例
	 * @param form
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("/assignObjList")
	public String assignObjList(ScrollImageSearchForm form,ShopSearchForm shopSearchForm, CasesSearchForm casesSearchForm, 
						ModelMap map, HttpServletRequest request){
		if(form != null && form.getScrollType() != null){
			try {
				//店铺
				if(form.getScrollType() == 1){
					//小区主键
					shopSearchForm.getOrderby().put("shopCreateTime", "desc");
					shopSearchForm.setShopStatus(1);
					//创建页面对象
					PageView<Shop> pageView = new PageView<Shop>(12, shopSearchForm.getPage());
					shopSearchForm.setFirstResult(pageView.getFirstResult());
					shopSearchForm.setMaxResult(pageView.getMaxresult());
					pageView.setQueryResult(shopService.getData(shopSearchForm));
					
					CitySearchForm cForm = new CitySearchForm();
					cForm.getOrderby().put("cityCreateTime", "desc");
					List<City> cityList = cityService.findAll(cForm);
					
					map.addAttribute("cityList", cityList);
					map.addAttribute("pageView", pageView);
					map.addAttribute("siform", form);
					map.addAttribute("form", shopSearchForm);
					map.addAttribute("reqUrl", "/control/scroll/scrollImageController/assignObjList.do");
					
					return "control/system/scroll/assignScrollShopList";
				}
				//案例
				if(form.getScrollType() == 2){
					casesSearchForm.getOrderby().put("caseCreateTime", "desc");
					casesSearchForm.setCasesStatus(1);
					//创建页面对象
					PageView<Cases> pageView = new PageView<Cases>(12, casesSearchForm.getPage());
					casesSearchForm.setFirstResult(pageView.getFirstResult());
					casesSearchForm.setMaxResult(pageView.getMaxresult());
					pageView.setQueryResult(casesService.getData(casesSearchForm));
					map.addAttribute("pageView", pageView);
					map.addAttribute("form", casesSearchForm);
					map.addAttribute("siform", form);
					map.addAttribute("reqUrl", "/control/scroll/scrollImageController/assignObjList.do");
					
					return "control/system/scroll/assignScrollCasesList";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "";
	}
	
	
	
	
	
	/**
	 * 列出首页滚动图管理
	 */
	@RequestMapping("/list")
	public String list(ScrollImageSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<ScrollImage> pageView = new PageView<ScrollImage>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(scrollImageService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加首页滚动图管理
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(ScrollImage scrollImage, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 添加首页滚动图管理
	 */
	@RequestMapping("/add")
	public String add(ScrollImage scrollImage, ModelMap map) {
		try {
			//TODO 设置scrollImage  eg:主键信息
			
			scrollImageService.save(scrollImage);
			return list(new ScrollImageSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示编辑首页滚动图管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(ScrollImageSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:ScrollImage scrollImage = scrollImageService.find(form.getId());
			//eg:map.addAttribute("entity", scrollImage);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑首页滚动图管理
	 */
	@RequestMapping("/edit")
	public String edit(ScrollImage ScrollImage, ModelMap map) {
		try {
			//TODO:处理首页滚动图管理
			
			
			scrollImageService.updateBySelective(ScrollImage);
			return list(new ScrollImageSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个首页滚动图管理
	 */
	@RequestMapping("/deletescrollImage")
	public String delete(ScrollImageSearchForm form, ModelMap map) {
		try {
			//TODO  eg:scrollImageService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除首页滚动图管理
	 */
	@RequestMapping("/batchDelete")
	public String deletes(ScrollImageSearchForm form, ModelMap map) {
		try {
//			TODO  eg:scrollImageService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
}
